﻿using System;
using System.Collections.Generic;
using System.Text;
using Xcap;
using Xcap.Xml;

namespace SIMcSDK
{
    public class XCAP
    {
        /// <summary>
        /// V tejto funkcii su spristupnene data,
        /// je to ako event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void xcap_DataRecievedEvent(object sender, DataRecievedEventArgs e)
        {
            //zobrazime status message
            Console.WriteLine(e.MessageStatus.ToString());

            //hlavicky zo servera
            Console.WriteLine(e.MessageHeaders);

            //xml obsah vrateny serverom => uzitocne informacie
            Console.Write(e.MessageXml);
        }

        /// <summary>
        /// Vrati cely resource lists
        /// </summary>
        public static void getContactLists(string host, int port, string accountUri, string password)
        {
            //konfiguracia
            Config config = new Config();
            config.Host = host;
            config.Port = port;
            config.UserAgent = "XCAP IMS Client";

            //kontaktne udaje
            Account account = new Account(accountUri, password);

            XcapClient xcap = new XcapClient(config, account);
            xcap.Connect();
            xcap.DataRecievedEvent += new DataRecievedEventHandler(xcap_DataRecievedEvent);

            XcapXmlResourceLists xml = new XcapXmlResourceLists();
            xcap.GetResourceLists();

            xcap.Disconnect();
        }

        public static void allowUser(string host, int port, string accountUri, string password, string addUri) {
            //konfiguracia
            Config config = new Config();
            config.Host = host;
            config.Port = port;
            config.UserAgent = "XCAP IMS Client";

            //kontaktne udaje
            Account account = new Account(accountUri, password);

            XcapClient xcap = new XcapClient(config, account);
            xcap.Connect();
            xcap.DataRecievedEvent += new DataRecievedEventHandler(xcap_DataRecievedEvent);

            //xcap.GetServerCapabilities();
            //xcap.GetPidf();
            //xcap.GetResourceLists();

            XcapXmlPresrules xml = new XcapXmlPresrules();
            //List<Contact> buddies = new List<Contact>();
            //buddies.Add(new Contact("janko@ahoj.sk", string.Empty));
            //buddies.Add(new Contact("jozko@ahoj.sk", string.Empty));
            //buddies.Add(new Contact("marienka@ahoj.sk", string.Empty));
            //xml.NewRule("allow-list", buddies, xcapPresRulesActions.allow, true);
            //xml.NewRule("confirm-list", buddies, xcapPresRulesActions.confirm, false);
            //xml.NewRule("polite-list", buddies, xcapPresRulesActions.polite_block, false);
            //xcap.PutPresRulesIdentity("polite-list", new Contact("sip:lampa.neposdna@ngnlab.eu"));
            //xcap.GetPresRules();
            xcap.PutPresRulesIdentity("allow-list", new Xcap.Xml.Contact(addUri));

            xcap.Disconnect();
        }

        public static void XcapUsageExamples()
        {
            //konfiguracia
            Config config = new Config();
            config.Host = "xcap.ngnlab.eu";
            config.Port = 8000;
            config.UserAgent = "XCAP IMS Client";

            //kontaktne udaje
            Account account = new Account("sip:rv_user2@ngnlab.eu","qwer56");

            XcapClient xcap = new XcapClient(config, account);
            xcap.Connect();
            xcap.DataRecievedEvent +=new DataRecievedEventHandler(xcap_DataRecievedEvent);
            
            //xcap.GetServerCapabilities();
            xcap.GetPidf();
            //xcap.GetResourceLists();

            #region resource-lists
            //XcapXmlResourceLists xml = new XcapXmlResourceLists();
            //List<Contact> buddies = new List<Contact>();
            ////buddies.Add(new Contact("sip:tanki@ahoj.sk", "Tankista"));
            ////buddies.Add(new Contact("sip:jozef@ahoj.sk", "Kukuc"));
            //////buddies.Add(new Contact("sip:jancika@ahoj.sk", "Stolicka"));
            ////xml.NewContactList("default", "Priatelia", buddies);
            ////xml.NewContactList("default1", "(NE)Priatelia", buddies);
            ////xcap.PutResourceLists(xml.GetResourceLists());
            ////xcap.PutResourceListsEntry("default1",new Contact("sip:stajgerss.p@ngnlag.eu", "Lampa svietiva"));
            ////xcap.GetResourceListsEntry("default1", new Contact("sip:jancsika@ahoj.sk"));
            ////xcap.DeleteResourceListsEntry("default1", new Contact("sip:jancika@ahoj.sk"));
            //xcap.GetResourceLists();
            #endregion

            #region presence information
            //XcapXmlPresence xml = new XcapXmlPresence("rv_user2@ngnlab.eu");
            //xml.NewTuple("qwer", xcapBasicStatus.open, "0915516811", "0.8", "Pristupny na telefone");
            //xml.NewPerson("ewq", string.Empty);
            //xml.NewDevice("asdf", "ID DEVICE", xcapBasicStatus.open, "som na zariadeni momentalne");

            //xcapRpidActivity[] acInput = { xcapRpidActivity.holiday, xcapRpidActivity.vacation, xcapRpidActivity.steering, xcapRpidActivity.other };
            //xml.rpidActivities(xml.Person, acInput, string.Empty, string.Empty);
            //xml.cipidElement(xml.Person, xcapCipidElement.homepage, "http://www.ra2tour.sk");
            //string content = xml.GetPresence();
            //xcap.PutPidf(content);
            //xcap.GetPidf();
            #endregion

            #region pres-rules
            //XcapXmlPresrules xml = new XcapXmlPresrules();
            //List<Contact> buddies = new List<Contact>();
            //buddies.Add(new Contact("janko@ahoj.sk", string.Empty));
            //buddies.Add(new Contact("jozko@ahoj.sk", string.Empty));
            //buddies.Add(new Contact("marienka@ahoj.sk", string.Empty));
            //xml.NewRule("allow-list", buddies, xcapPresRulesActions.allow, true);
            //xml.NewRule("confirm-list", buddies, xcapPresRulesActions.confirm, false);
            //xml.NewRule("polite-list", buddies, xcapPresRulesActions.polite_block, false);
            //xcap.PutPresRulesIdentity("polite-list", new Contact("sip:lampa.neposdna@ngnlab.eu"));
            //xcap.GetPresRules();
            //xcap.GetPresRulesIdentity("polite-list", new Contact("janko@ahoj.sk"));
            //Console.WriteLine(xml.GetPresRules());
            #endregion
            
            //Console.WriteLine(xml.GetResourceLists());
            //xcap.PutPresRules(xml.GetPresRules());
            //Console.WriteLine(content);
            
            //xcap.GetResourceLists();
            

            xcap.Disconnect();
        }
    }
}
